package com.lzj.demo.jdbc;

import cn.hutool.core.io.resource.ClassPathResource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Properties;

public class BrandAdd {
    @Test
    public void test() throws Exception {
        //接收页面提交的参数
        String brandName = "香飘飘";
        String companyName = "香飘飘";
        String description = "绕地球一圈";
        int ordered = 1;
        int status = 1;

        //获取定义在src/main/resources文件夹中的配置文件
        ClassPathResource resource = new ClassPathResource("druid.properties");
        Properties prop = new Properties();
        prop.load(resource.getStream());
        //获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        //获取数据库连接
        Connection conn = dataSource.getConnection();

        //定义insert sql语句
        String sql = "insert into tb_brand(id,brand_name, company_name, ordered, description, status) values(?,?,?,?,?,?);";

        //设置参数
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setInt(1,3);
        preparedStatement.setString(2, brandName);
        preparedStatement.setString(3, companyName);
        preparedStatement.setInt(4, ordered);
        preparedStatement.setString(5, description);
        preparedStatement.setInt(6, status);

        //执行SQL,并返回被影响的行数
        int count = preparedStatement.executeUpdate();
        System.out.println(count > 0);
        //释放资源
        preparedStatement.close();
        conn.close();
    }
}